package t::MYTEST;

# -Base部分在调用t::ANYUTester时传入
use Test::Nginx::Socket -Base;

repeat_each(2);
log_level('info');

# 控制block测试顺序与代码一致（默认是随机测试）
no_shuffle();

# 使用diff风格的字符串对比(response_body/grep_error_log_out等)
no_long_string();

# 在每个block启动前调用进行预处理。
add_block_preprocessor(sub {
    my ($block) = @_;

    # 修改http部分配置
    my $http_config = $block->http_config;
    # EOCH为perl的多行字符串语法
    $http_config .= <<'_EOCH_';
    lua_package_path '/Users/admin/application/service/?.lua;;/Users/admin/application/service/core/?.lua;;/Users/admin/application/service/http/?.lua;;/Users/admin/application/service/plugins/?.lua;;/Users/admin/application/service/stream/?.lua;;/Users/admin/application/service/utils/?.lua;;/Users/admin/application/?/init.lua;;/Users/admin/application/service/utils/prometheus/?.lua;;';
    lua_package_cpath '/Users/admin/application/service/?.so;;';
#    lua_package_path "/Users/admin/application/?.lua;;";
    lua_code_cache on;
    sendfile        on;
    lua_shared_dict worker-events 10m;
    lua_shared_dict  redis-cluster-slot-lock 10m;
    lua_shared_dict  prometheus_metrics 200m;
    lua_shared_dict  frequency_limit 200m;
    lua_shared_dict  limit_black_dict 200m;
    lua_shared_dict  white_filter 1024m;
    lua_shared_dict  black_filter 1024m;
    resolver 8.8.8.8;
    init_by_lua_block {
            ngx.config.prefix = "/Users/admin/application/"
            local app = require("service")
            app.http_init()
            -- local v = require "jit.v"
            -- v.on("./jit.log")
    }

    init_worker_by_lua_block {
            local app = require("service")
            app.http_init_worker()

    }
    access_by_lua_block {
        local app = require("service")
        app.http_access()
    }
    #content_by_lua_block {
    #    local app = require("service")
    #    app.http_content()
    #}
    header_filter_by_lua_block {
        local app = require("service")
        app.http_header_filter()
    }
    body_filter_by_lua_block {
        local app = require("service")
        app.http_body_filter()
    }
    log_by_lua_block {
        local app = require("service")
        app.http_log()
    }
    server {
      listen 9145;
      location = /nginx_status {
          allow 127.0.0.1;
          allow 192.168.0.0/16;
          deny all;
          access_log off;
          stub_status;
      }

    }
_EOCH_
    $block->set_value("http_config", $http_config);

    # 修改server部分配置
    my $config = $block->config;
    $config .= <<'_EOCS_';
    location /cc_cache {
        rewrite_by_lua_file /home/work/cc/cc_nginx/luafiles/cc/api/cache.lua;
    }

    location /rule_check {
        rewrite_by_lua_block {
            local host = ngx.var.arg_host
            local module = ngx.var.arg_module
            local uri = ngx.var.arg_uri
            local infos = {
                host = host,
                uri = uri,
            }

            local rule_api = require "cc.rule.cache"
            local strategy_api = require "cc.strategy.cache"

            local key = module .. ":" .. host
            local rules = strategy_api.lru_get(key)
            if rules then
                ngx.print(rule_api.match(infos, rules))
            else
                ngx.print("miss")
            end
        }
    }
_EOCS_
    $block->set_value("config", $config);

});

1;